package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.factory.DAOFactory;
import com.vo.Message;
import com.vo.Revert;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;

@WebServlet("/Note")
public class NoteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getParameter("data");
        JSONObject jsonObj = new JSONObject();
        JSONArray jsonArray = null;
        jsonObj.put("status", 0);
        if (data != null && !("".equals(data))) {
            jsonObj.put("status", 1);
            switch (data) {
                case "MessageByPage":
                    jsonArray = DAOFactory.getNoteDAOInstance().getMessageByPage(
                            Integer.parseInt(req.getParameter("page")),
                            Integer.parseInt(req.getParameter("limit"))
                    );
                    jsonObj.put("list", jsonArray);
                    break;
                case "Search":
                    jsonArray = DAOFactory.getNoteDAOInstance().getInfoByKeyWord(
                            req.getParameter("keyWord")
                    );
                    jsonObj.put("list", jsonArray);
                    break;
                case "Revert":
                    jsonArray = DAOFactory.getNoteDAOInstance().getRevertByMessageID(
                            Integer.parseInt(req.getParameter("messageID"))
                    );
                    jsonObj.put("list", jsonArray);
                    break;
                default:
                    break;
            }
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(jsonObj.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int messageID;
        String title;
        String content;
        String writer;
        Timestamp writeDate;
        Integer count = null;
        int revertID;
        Message message = null;
        Revert revert = null;

        String operate = req.getParameter("operate");
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("status", 0);
        jsonObj.put("info", "操作选项参数错误");
        String uname = (String) req.getSession().getAttribute("uname");
        if (operate != null && !("".equals(operate)) && uname != null) {
            switch (operate) {
                case "updateMessage":
                    writer = req.getParameter("writer");
                    if (uname.equals(writer)) {
                        jsonObj.put("status", 1);
                        messageID = Integer.parseInt(req.getParameter("messageID"));
                        title = req.getParameter("title");
                        content = req.getParameter("content");
                        writeDate = Timestamp.valueOf(req.getParameter("writeDate"));
                        count = Integer.parseInt(req.getParameter("count"));
                        message = new Message(messageID, title, content, writer, writeDate, count);
                        if (DAOFactory.getNoteDAOInstance().updateMessage(message)) {
                            jsonObj.put("flag", 1);
                            jsonObj.put("info", "成功更新留言");
                        } else {
                            jsonObj.put("flag", 0);
                            jsonObj.put("info", "更新留言失败");
                        }
                    } else {
                        jsonObj.put("info", "无操作权限");
                    }
                    break;
                case "addMessage":
                    writer = req.getParameter("writer");
                    if (uname.equals(writer)) {
                        jsonObj.put("status", 1);
                        messageID = 0;
                        title = req.getParameter("title");
                        content = req.getParameter("content");

                        writeDate = Timestamp.valueOf(req.getParameter("writeDate"));
                        count = Integer.parseInt(req.getParameter("count"));
                        message = new Message(messageID, title, content, writer, writeDate, count);
                        if (DAOFactory.getNoteDAOInstance().addMessage(message)) {
                            jsonObj.put("flag", 1);
                            jsonObj.put("info", "成功添加留言");
                        } else {
                            jsonObj.put("flag", 0);
                            jsonObj.put("info", "添加留言失败");
                        }
                    } else {
                        jsonObj.put("info", "无操作权限");
                    }
                    break;
                case "deleteMessage":
                    messageID = Integer.parseInt(req.getParameter("messageID"));
                    writer = DAOFactory.getNoteDAOInstance().getMessageWriterByID(messageID);
                    if (uname.equals(writer)) {
                        jsonObj.put("status", 1);
                        if (DAOFactory.getNoteDAOInstance().deleteMessage(messageID)) {
                            jsonObj.put("flag", 1);
                            jsonObj.put("info", "成功删除留言");
                        } else {
                            jsonObj.put("flag", 0);
                            jsonObj.put("info", "删除留言失败");
                        }
                    } else {
                        jsonObj.put("info", "无操作权限");
                    }
                    break;
                case "updateRevert":
                    writer = req.getParameter("writer");
                    if (uname.equals(writer)) {
                        jsonObj.put("status", 1);
                        revertID = Integer.parseInt(req.getParameter("revertID"));
                        messageID = Integer.parseInt(req.getParameter("messageID"));
                        content = req.getParameter("content");

                        writeDate = Timestamp.valueOf(req.getParameter("writeDate"));
                        revert = new Revert(revertID, messageID, content, writer, writeDate);
                        if (DAOFactory.getNoteDAOInstance().updateRevert(revert)) {
                            jsonObj.put("flag", 1);
                            jsonObj.put("info", "成功更新回复言");
                        } else {
                            jsonObj.put("flag", 0);
                            jsonObj.put("info", "更新回复失败");
                        }
                    } else {
                        jsonObj.put("info", "无操作权限");
                    }
                    break;
                case "addRevert":
                    writer = req.getParameter("writer");
                    if (uname.equals(writer)) {
                        jsonObj.put("status", 1);
                        revertID = 0;
                        messageID = Integer.parseInt(req.getParameter("messageID"));
                        content = req.getParameter("content");
                        writeDate = Timestamp.valueOf(req.getParameter("writeDate"));
                        revert = new Revert(revertID, messageID, content, writer, writeDate);
                        if (DAOFactory.getNoteDAOInstance().addRevert(revert)) {
                            jsonObj.put("flag", 1);
                            jsonObj.put("info", "成功添加回复");
                        } else {
                            jsonObj.put("flag", 0);
                            jsonObj.put("info", "添加回复失败");
                        }
                    } else {
                        jsonObj.put("info", "无操作权限");
                    }
                    break;
                case "deleteRevert":
                    revertID = Integer.parseInt(req.getParameter("revertID"));
                    writer = DAOFactory.getNoteDAOInstance().getRevertWriterByID(revertID);
                    if (uname.equals(writer)) {
                        jsonObj.put("status", 1);
                        if (DAOFactory.getNoteDAOInstance().deleteRevert(revertID)) {
                            jsonObj.put("flag", 1);
                            jsonObj.put("info", "成功删除回复");
                        } else {
                            jsonObj.put("flag", 0);
                            jsonObj.put("info", "删除回复失败");
                        }
                    } else {
                        jsonObj.put("info", "无操作权限");
                    }
                    break;
                default:
                    break;
            }
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(jsonObj.toString());
    }
}
